package dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.model.SelectItem;

import org.hibernate.Session;

import pojos.Client;
import pojos.Sport;
import pojos.Voyage;

public class Service {
	
	private List promotions;
	private List<SelectItem> sports;
	private List<SelectItem> transports;
	private ArrayList<String> transports_tmp;
	private List<SelectItem> ville_depart;
	private ArrayList<String> ville_depart_tmp;
	private List<SelectItem> ville_arrivee;
	private ArrayList<String> ville_arrivee_tmp;
	
	public static Long getLastAdresseId(){
		Session session = HibernateUtil.getSession();		
		List list = session.createQuery("select max(id_adresse) from Adresse").list();
		Long idAdresse = (Long)list.get(0);
		return idAdresse;
	}
	
	public static Sport getSportById(long id){
		Session session = HibernateUtil.getSession();		
		List list = session.createQuery("from Sport where id_sport =" + id).list();
		Sport sport= (Sport)list.get(0);
		return sport;
	}
	
	public static Voyage getVoyageById(long id){
		Session session = HibernateUtil.getSession();		
		List list = session.createQuery("from Voyage where id_voyage =" + id).list();
		Voyage voyage= (Voyage)list.get(0);
		return voyage;
	}

	public List getPromotions(){
		Session session = HibernateUtil.getSession();
		promotions = new ArrayList<Voyage>();
		List lst = session.createQuery("from Voyage where promotion=1 order by date_depart asc").list();
		for (Iterator iterator = lst.iterator(); iterator.hasNext();) {
			Voyage voyage = (Voyage) iterator.next();
			promotions.add(voyage);
		}
		return promotions;
	}
	
	public List<SelectItem> getSports(){
		Session session = HibernateUtil.getSession();
		sports = new ArrayList<SelectItem>();
		List lst = session.createQuery("from Sport ").list();
		for (Iterator iterator = lst.iterator(); iterator.hasNext();) {
			Sport sport1 = (Sport) iterator.next();
			sports.add(new SelectItem (sport1.getNom()));
		}
		return sports;
	}
	
	public List<SelectItem> getTransports() {
		
		boolean repetition_transport=false;
		transports_tmp = new ArrayList<String>();
		
		Session session = HibernateUtil.getSession();
		transports = new ArrayList<SelectItem>();
		List lst = session.createQuery("from Voyage ").list();
		for (Iterator iterator = lst.iterator(); iterator.hasNext();) {
			Voyage transport = (Voyage) iterator.next();
			
			for(int i=0;i<transports.size();i++)
			{				
				if (transports_tmp.get(i).equals(transport.getTransport()))
					repetition_transport = true;
			}
			
			if (repetition_transport== false)
			{
				transports_tmp.add(transport.getTransport());
				transports.add(new SelectItem(transport.getTransport()));
			}	
				
			repetition_transport = false;
		}	
			
		return transports;
	}
	
	
	public List<SelectItem> getVille_depart() {
		
		boolean repetition_ville=false;
		ville_depart_tmp = new ArrayList<String>();
		
		Session session = HibernateUtil.getSession();
		ville_depart = new ArrayList<SelectItem>();
		List lst = session.createQuery("from Voyage ").list();
		for (Iterator iterator = lst.iterator(); iterator.hasNext();) {
			Voyage ville_dep = (Voyage) iterator.next();
			
			for(int i=0;i<ville_depart.size();i++)
			{				
				if (ville_depart_tmp.get(i).equals(ville_dep.getVille_depart()))
					repetition_ville = true;
			}
			
			if (repetition_ville== false)
			{
				ville_depart_tmp.add(ville_dep.getVille_depart());
				ville_depart.add(new SelectItem(ville_dep.getVille_depart()));
			}	
				
			repetition_ville = false;
		}	
		return ville_depart;
	}
	
	
	public List<SelectItem> getVille_arrivee() {
		
		boolean repetition_ville=false;
		ville_arrivee_tmp = new ArrayList<String>();
		
		Session session = HibernateUtil.getSession();
		ville_arrivee = new ArrayList<SelectItem>();
		List lst = session.createQuery("from Voyage ").list();
		for (Iterator iterator = lst.iterator(); iterator.hasNext();) {
			Voyage ville_arr = (Voyage) iterator.next();
			
			for(int i=0;i<ville_arrivee.size();i++)
			{				
				if (ville_arrivee_tmp.get(i).equals(ville_arr.getDestination()))
					repetition_ville = true;
			}
			
			if (repetition_ville== false)
			{
				ville_arrivee_tmp.add(ville_arr.getDestination());
				ville_arrivee.add(new SelectItem(ville_arr.getDestination()));
			}	
				
			repetition_ville = false;
		}		
		return ville_arrivee;
	}

	
	public static long getSportId(String nomSport){
		Session session = HibernateUtil.getSession();		
		List list = session.createQuery("select id_sport from Sport where nom='"+nomSport+"'").list();
		Long idSport = (Long)list.get(0);
		return idSport;
	}
	
	public static Client getClientByLoginMotDePasse(String login, String mot_de_passe){
		
		Session session = HibernateUtil.getSession();		
		List list = session.createQuery("from Client where login='"+login+"' and mot_de_passe = '" + mot_de_passe + "'").list();
		if(list.size()>0){
			return (Client) list.get(0);
		}else{
			return new Client();
		}
		
	}
}

